package belf.migrate.engine.dao;

import belf.migrate.api.job.Job;
import belf.migrate.api.taskconf.Task;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 从数据库中查询控制台配置的{@link Job}信息
 */
@Slf4j
public class JobDAO extends BaseDAO<Job> {

    @Override
    public void save(Job job) {
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = getConnection();
            String sql = "INSERT INTO migration_job" +
                    "(task_id,owner,create_time,start_time,missions)" +
                    " values(?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setLong(1, job.getTask().getTaskId());
            ps.setString(2, job.getOwner());
            ps.setDate(3, new java.sql.Date(job.getCreateTime().getTime()));
            ps.setDate(4, new java.sql.Date(job.getStartTime().getTime()));
            ObjectMapper mapper = new ObjectMapper();
            ps.setString(5, mapper.writeValueAsString(job.getMissions()));

            ps.executeUpdate();
            ps.close();
            log.info("Save Job to db: {}", job.getTask().getName() + "->" + job.getOwner());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != ps) {
                    ps.close();
                }
                if (null != connection) {
                    connection.close();
                }
            } catch (SQLException e) {}
        }
    }

    @Override
    public Job queryById(long id) {
        Job job = null;
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = getConnection();
            String sql = "SELECT * FROM migration_job WHERE job_id=?";
            ps = connection.prepareStatement(sql);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Task task = new TaskDAO().queryById(rs.getInt("task_id"));
                ObjectMapper mapper = new ObjectMapper();
                List<String> missions = mapper.readValue(rs.getString("missions"), ArrayList.class);
                job = new Job(rs.getLong("job_id"),
                        task,
                        rs.getString("owner"),
                        rs.getDate("create_time"),
                        rs.getDate("start_time"),
                        null,
                        missions);

            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != ps) {
                    ps.close();
                }
                if (null != connection) {
                    connection.close();
                }
            } catch (SQLException e) {}
        }
        return job;
    }

    @Override
    public void deleteById(long id) {
        log.info("Delete Job by id: {}", id);
        String sql = "DELETE FROM migration_job where job_id=" + id;
        executeSql(sql);
    }
}
